package com.hzcx.app.simplechat.ui.publicui;

import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.login.ForgetPwdActivity;
import com.hzcx.app.simplechat.ui.login.bean.LoginInfoBean;
import com.hzcx.app.simplechat.ui.login.util.LoginUtil;
import com.hzcx.app.simplechat.ui.publicui.bean.ProtocolBean;
import com.hzcx.app.simplechat.ui.publicui.contract.SendCodeContract;
import com.hzcx.app.simplechat.ui.publicui.enums.SendCodeEnum;
import com.hzcx.app.simplechat.ui.publicui.presenter.SendCodePresenter;
import com.hzcx.app.simplechat.ui.setting.UpdatePhoneActivity;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.view.PhoneCodeEditText;
import com.hzcx.app.simplechat.view.SendCodeButton;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 通用的验证码界面
 */
public class SendCodeActivity extends BaseActivity<SendCodePresenter> implements PhoneCodeEditText.OnPhoneCodeListener, SendCodeContract.View {

    public static final String INTENT_EUNM = "INTENT_EUNM";
    public static final String INTENT_PHONE = "INTENT_PHONE";
    public static final String INTENT_PHONE_CITY = "INTENT_PHONE_CITY";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_phone_code)
    PhoneCodeEditText etPhoneCode;
    @BindView(R.id.btn_send_code)
    SendCodeButton btnSendCode;

    private String codeEnum;
    private String phone;
    private String phoneCity;

    @Override
    public int getLayout() {
        return R.layout.activity_send_code;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SendCodePresenter();
    }

    @Override
    public void initView() {

        // 弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        tvTitle.setText("手机号验证");

        etPhoneCode.setOnPhoneCodeListener(this);
    }

    @Override
    public void initData() {
        codeEnum = getIntent().getStringExtra(INTENT_EUNM);
        phone = getIntent().getStringExtra(INTENT_PHONE);
        phoneCity = getIntent().getStringExtra(INTENT_PHONE_CITY);
        tvPhone.setText(phoneCity + "    " + AppHelper.hidePhone(phone));

        mPresenter.sendCode(this, phone, codeEnum, phoneCity);
    }

    @OnClick({R.id.btn_send_code, R.id.tv_code_fail})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_send_code:
                mPresenter.sendCode(this, phone, codeEnum, phoneCity);
                break;

            case R.id.tv_code_fail: // 收不到验证码
                mPresenter.getProtocol(this, "ver_code");
                break;
        }
    }


    /**
     * 验证码输入完成
     *
     * @param code
     */
    @Override
    public void complte(String code) {
        switch (codeEnum) {
            case SendCodeEnum.LOGIN: // 登录
                mPresenter.loginByCode(this, phone, etPhoneCode.getPhoneCode(), phoneCity);
                break;

            case SendCodeEnum.UPDATE_PHONE: // 修改手机号
            case SendCodeEnum.FORGET_PWD: // 忘记密码
                mPresenter.checkCode(this, phone, etPhoneCode.getPhoneCode(), codeEnum);
                break;
        }
    }

    /**
     * 发送成功
     */
    @Override
    public void sendSuccess() {
        showError("发送成功");
        btnSendCode.start();
    }

    /**
     * 验证码发送失败
     */
    @Override
    public void sendFail() {
        btnSendCode.sendFinish();
    }

    /**
     * 登录成功
     *
     * @param data
     */
    @Override
    public void loginSuccess(LoginInfoBean data) {
        LoginUtil.loginSuccess(this, data);
    }

    /**
     * 验证码验证成功
     */
    @Override
    public void checkCodeSuccess() {
        switch (codeEnum) {
            case SendCodeEnum.UPDATE_PHONE: // 修改手机号
                startActivity(new Intent(this, UpdatePhoneActivity.class));
                break;

            case SendCodeEnum.FORGET_PWD: // 忘记密码
                startActivity(new Intent(this, ForgetPwdActivity.class)
                        .putExtra(ForgetPwdActivity.INTENT_PHONE, phone)
                        .putExtra(ForgetPwdActivity.INTENT_CODE, etPhoneCode.getPhoneCode()));
                break;
        }
        finish();
    }

    /**
     * 收不到验证码地址获取成功
     *
     * @param data
     */
    @Override
    public void protocolResult(ProtocolBean data) {
        startActivity(new Intent(this, WebViewActivity.class)
                .putExtra(WebViewActivity.INTENT_TITLE, data.getName())
                .putExtra(WebViewActivity.INTENT_WEB_URL, data.getContenturl()));
    }
}
